package bid.yanhj.yanhaijiang.handler;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**
 * message 信息组件  主要用在线程间传递
 *
 * handler 传递信心的组件 主要用在线程间的信息传递
 *
 * 1 利用匿名内部类实心handler
 * 2 在需要的地方发送handler
 * 3 在handler内部类得到message对象传递的信息
 *
 * 另一种方式 继承handler实现
 *
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button innerClassBtn, extendsBtn;

    private Handler innerHandler;

    private MyHandler myHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        innerClassBtn = findViewById(R.id.main_inner_class_btn);
        extendsBtn = findViewById(R.id.mian_extends_btn);

        innerClassBtn.setOnClickListener(this);
        extendsBtn.setOnClickListener(this);

        // import android.os.Handler;
        innerHandler= new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                String currentThreadName = Thread.currentThread().getName();
                String data = (String) msg.obj;
                Log.d("yanhj", currentThreadName + ":" + data);
                return false;
            }
        });

        // 在主线程创建handler
        myHandler = new MyHandler();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.main_inner_class_btn:

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String currentThreadName = Thread.currentThread().getName();
                        String data = currentThreadName + "--" + "这是内部类形式";

                        // 发送
                        Message message = new Message();
                        message.what = 1;
                        message.obj = data;
                        innerHandler.sendMessage(message);
                    }
                }).start();
                break;
            case R.id.mian_extends_btn:

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String currentThreadName = Thread.currentThread().getName();
                        String data = currentThreadName + "--" + "这是继承的形式";

                        // 发送
                        Message message = new Message();
                        message.what = 1;
                        message.obj = data;
                        myHandler.sendMessage(message);
                    }
                }).start();
                break;
        }
    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            String currentThreadName = Thread.currentThread().getName();
            String data = (String) msg.obj;
            Log.d("yanhj", currentThreadName + ":" + data);
            super.handleMessage(msg);
        }
    }
}
